package edu.usm.cs.roberge;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The underlying application data model
 * It's important to note that this class does not actually populate the
 * data. Instead, it is up another class(es) to fill the actual data.
 * This class does provide the ability to persist data across application
 * runs
 */
public class NewsReaderModel implements Externalizable {

	private List<NewsGroup> newsGroups = new ArrayList<NewsGroup>();
	
	@Override
	public void readExternal(ObjectInput arg0) throws IOException, ClassNotFoundException {
		newsGroups = (List<NewsGroup>) arg0.readObject();
	}

	@Override
	public void writeExternal(ObjectOutput arg0) throws IOException {
		arg0.writeObject(newsGroups);
	}

	public void addNewsGroup(NewsGroup ng) {
		newsGroups.add(ng);
	}

	public int getNewsGroupSize() {
		return newsGroups.size();
	}

	public NewsGroup getNewsGroup(int index) {
		return newsGroups.get(index);
	}

	public List<NewsGroup> getNewsGroups() {
		return Collections.unmodifiableList(newsGroups);
	}

	public List<NewsGroup> getFavorites() {
		List<NewsGroup> favorites = new ArrayList<NewsGroup>();
		
		for(NewsGroup ng : newsGroups) {
			if (ng.isFavorite()) {
				favorites.add(ng);
			}
		}
		
		return favorites;
	}
}
